import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { tokenStore } from '../../stores/tokenStore'
import { currentUserStore } from '../../stores/currentUserStore'
import { userGetHomePageRecommendation, userGetUserdetail } from "../../api/userService"
import Notify from '@vant/weapp/notify/notify'
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 推荐用户列表
    recommendUsers: [],
    // 是否正在加载推荐用户
    loading: false,
    // 是否为空结果
    isEmpty: false,
    // 是否正在刷新推荐
    refreshing: false,
    // 用户详细信息页
    showUserDetail: false,
    userDescription: '',
    userWeChatId: '',
    userQQId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.tokenStoreBinding = createStoreBindings(this, {
      store: tokenStore,
      fields: ['token'],
      actions: ['updateToken']
    })
    this.currentUserStoreBinding = createStoreBindings(this, {
      store: currentUserStore,
      fields: ['id', 'username', 'gender', 'avatar', 'description', 'openId', 'wechatId', 'tags', 'createTime', 'qqId'],
      actions: ['updateUserName', 'updateGender', 'updateAvatar', 'updateDescription', 'updateWeChatId', 'updateQQId', 'updateTags', 'updateCurrentUser']
    })
    // 没登录，直接打到登录页
    if (!this.data.token) {
      wx.switchTab({
        url: '/pages/user/user'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取推荐用户
    if (this.data.token) {
      // console.log('初始化')
      this.getRecommendUsers()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 页面卸载时销毁store实例
    this.tokenStoreBinding.destroyStoreBindings()
    this.currentUserStoreBinding.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 获取推荐用户
  async getRecommendUsers() {
    this.setData({
      loading: true,
      isEmpty: false,
    })
    // console.log(this.data.tags)
    const result = await userGetHomePageRecommendation(this.data.tags)
    if (result.code === 200) {
      this.setData({
        recommendUsers: result.data,
        loading: false,
        isEmpty: result.data.length === 0,
        refreshing: false
      })
    } else {
      this.setData({
        loading: false,
        isEmpty: true,
        refreshing: false
      })
    }
  },

  // 刷新推荐
  async refreshRecommend() {
    if (this.data.refreshing) {
      return
    }

    this.setData({
      refreshing: true,
    })
    this.getRecommendUsers()
  },

  // 处理联系用户事件
  async handleContact(e) {
    
    const { userId } = e.detail
    // console.log("联系用户:", userId)
    const result = await userGetUserdetail(userId)
    if (result.code === 200) {
      this.setData({
        userDescription: result.data.description,
        userWeChatId: result.data.wechatId,
        userQQId: result.data.qqId
      })
      // 展示弹窗
      this.setData({
        showUserDetail: true
      })
    }
  },

  onUserDetailClose() {
    this.setData({
      showUserDetail: false,
    })
    this.setData({
      userDescription: '',
      userWeChatId: '',
      userQQId: ''
    })
  }
})